<?php
require_once("config.php");
require_once("nusoap/nusoap.php");
require_once("utils.php");
require_once("Component.php");
require_once("db/dbutils.php");

class Opportunity extends Component {

	var $defaultColumns;
	var $columnMappings;
		
        function __construct($proxy) {

		$this->defaultColumns = array(
                                        'name',
                                        'account_name',										                                     
                                        'amount',
                                        'sales_stage',
                                        'lead_source',
                                        'assigned_user_id',                                       
                                        'description',                                        
                                        );

		$this->columnMappings = array ();


		parent::__construct("Opportunities", "opportunities", $proxy);
		
        }

	function getRecords(){

		$customQuery = null;

		if ($this->keysOnly == null) {

			$query = $this->getQueryColumns($this->defaultColumns);
			$query = str_replace('opportunities.account_name','accounts.name as account_name, accounts.id as account_id', $query);			
            $query = 'select '.$query.' from opportunities ';
			$query .=  ', accounts ,accounts_opportunities' ;
	
			$opportunity_id = isset($_REQUEST['opportunity_id']) ? $_REQUEST['opportunity_id'] : null;

			$customQuery =  ($opportunity_id != null) ? " opportunities.id=".$this->quote.$opportunity_id.$this->quote : null;
		} else {
			$query = "select id from opportunities";
		}
		$customQuery .= ' accounts.id=accounts_opportunities.account_id and opportunities.id=accounts_opportunities.opportunity_id';
		return $this->getRows($query, $customQuery, " accounts.date_modified desc ", $this->columnMappings);		

	}
	function getModuleRecords($module){		
		if($module!='users')
        	$query = "select id,name from $module where deleted=0 order by name asc" ;
        else {
        	$query = "select id,user_name as name from users where deleted=0 order by name asc" ;			
        }
		return $this->getRow($query);	
	}

	function getRecord($condition){
        $query = $this->getQueryColumns($this->defaultColumns);
        $query = str_replace('opportunities.account_name','accounts.name as account_name, accounts.id as account_id', $query);
        $query = 'select '.$query.' from opportunities ';        
		$query .=  ', accounts ,accounts_opportunities';
		$query .=  ' WHERE '.$condition;
		$query .= ' and accounts.id=accounts_opportunities.account_id and opportunities.id=accounts_opportunities.opportunity_id';                
	
        return $this->getRow($query);
        }


	function getRecordCount() {
		$query = 'select count(*) row_count from opportunities, accounts_opportunities ';
        $opportunity_id = isset($_REQUEST['opportunity_id']) ? $_REQUEST['opportunity_id'] : null;
        $customQuery =  ($opportunity_id != null) ? " accounts.id=".$this->quote.$opportunity_id.$this->quote : null;
        $customQuery .= " opportunities.id = accounts_opportunities.opportunity_id";
		return $this->getTotalRowCount($query, $customQuery, $this->columnMappings);
	}

	function saveRecord($id, $name, $account_id,$account_name, $amount, $sales_stage,
							 $lead_source, $assigned_user_id, $description) {
							 	
	/*
	 * As the SOAP request is not working for insert/update record, we are using direct query as bitzermobile is using in login and other queries.
	 * 
	 *  
	 */
		CreateDBConnection();
		if($id)
		{
			$query = "UPDATE opportunities set name='$name',amount='$amount',sales_stage='$sales_stage',
			lead_source='$lead_source',assigned_user_id='$assigned_user_id',description='$description',date_modified=CURRENT_TIMESTAMP WHERE id='$id'";
			$queryResult = mysql_query($query) ;
			
			$affected = mysql_affected_rows();
			
			$query = "UPDATE accounts_opportunities set account_id='$account_id',date_modified=CURRENT_TIMESTAMP WHERE opportunity_id='$id'";
			$queryResult = mysql_query($query) ;
			
			$affected += mysql_affected_rows();			
			return $affected;
		}
		else {
			$opp_id = create_guid();
			$amount = number_format($amount,2);
			$query = "INSERT INTO opportunities (id,name,amount,sales_stage,lead_source,assigned_user_id,description,date_entered,date_modified) 
			VALUES('$opp_id','$name',$amount,'$sales_stage','$lead_source','$assigned_user_id','$description',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)";
			$queryResult = mysql_query($query) ;
			
			
		
			$ac_id = create_guid();	
			$query = "INSERT INTO accounts_opportunities (id,account_id,opportunity_id,date_modified) 
			VALUES('$ac_id','$account_id','$opp_id',CURRENT_TIMESTAMP)";
			$queryResult = mysql_query($query) ;
			
			return true;
		}
							 	
		exit;
	/* end */						 	
			if ($id) {
				$response = $this->proxy->set_entry($this->sess, 'Opportunities', 
								array(array("name" => 'id',"value" => $id),
									  array("name" => 'name',"value" => $name),
								      array("name" => 'account_name',"value" => $account_name),
            					      array("name" => 'amount',"value" => $amount),
									  array("name" => 'sales_stage',"value" => $sales_stage),
									  array("name" => 'lead_source',"value" => $lead_source),
									  array("name" => 'assigned_user_id',"value" => $assigned_user_id),									  
									  array("name" => 'description',"value" => $description),									  
            						 )
            				);
			} else {
				$response = $this->proxy->set_entry($this->sess, 'Opportunities', 
								array(
									  array("name" => 'name',"value" => $name),
								      array("name" => 'account_name',"value" => $account_name),
            					      array("name" => 'amount',"value" => $amount),
									  array("name" => 'sales_stage',"value" => $sales_stage),
									  array("name" => 'lead_source',"value" => $lead_source),
									  array("name" => 'assigned_user_id',"value" => $assigned_user_id),									  
									  array("name" => 'description',"value" => $description),									  
            						 )
            				);
            }				 
			return $response;
		}
		
	}
$component = new Opportunity($proxy);

$sales_stageArray = array("" => "",
					   "Prospecting" => "Prospecting",
					   "Qualification" => "Qualification",					   
					   "Needs Analysis" => "Needs Analysis",
					   "Value Proposition" => "Value Proposition",
					   "Id. Decision Makers" => "Id. Decision Makers",
					   "Perception Analysis" => "Perception Analysis",
					   "Proposal/Price Quote" => "Proposal/Price Quote",
					   "Negotiation/Review" => "Negotiation/Review",
					   "Closed Won" => "Closed Won",
					   "Closed Lost" => "Closed Lost",
					  );
$lead_sourceArray = array("" => "",
					   "Cold Call" => "Cold Call",
					   "Existing Customer" => "Existing Customer",					   
					   "Self Generated" => "Self Generated",
					   "Employee" => "Employee",
					   "Partner" => "Partner",
					   "Public Relations" => "Public Relations",
					   "Direct Mail" => "Direct Mail",
					   "Conference" => "Conference",
					   "Trade Show" => "Trade Show",
					   "Web Site" => "Web Site",
					   "Email" => "Email",
					   "Campaign" => "Campaign",
					   "Other" => "Other",
					  );
?>